/**
 * @file ChangeColorMode.cpp
 *
 * @date 23/12/2011
 * @author Tales 
 *
 */

#include "ChangeColorMode.hpp"
#include <sstream>
#include "../controller/Editor.hpp"

namespace yep
{

static const int STATE_PRESENTATION = 0, STATE_ASK = 1, STATE_R = 2, STATE_G = 3, STATE_B = 4, STATE_END = -1;

pair<string, ConsoleModeBase::Function> ChangeColorMode::getNextLine(Editor & editor)
{
	string text;
	Function function;

	switch (state)
	{
	case STATE_PRESENTATION:
	{
		stringstream s;
		Color actual = editor.getColor();
		s << "A cor atual é: {R = " << (size_t)actual.red() << ", G = " << (size_t)actual.green() << ", B = " << (size_t)actual.blue() << "}";
		s.flush();
		text = s.str();
		function = NO_FUNCTION;
		state = STATE_ASK;
		break;
	}
	case STATE_ASK:
		text = "Deseja trocar a cor? (S/N): ";
		function = INPUT_YESNO;
		break;
	case STATE_R:
		text = "R (0 <= x <= 255): ";
		function = INPUT_INT;
		break;
	case STATE_G:
		text = "G (0 <= x <= 255): ";
		function = INPUT_INT;
		break;
	case STATE_B:
		text = "B (0 <= x <= 255): ";
		function = INPUT_INT;
		break;
	case STATE_END:
		function = END;
		break;
	}

	return make_pair(text, function);
}

void ChangeColorMode::handleAnswer(Editor & editor, string answer)
{
	switch (state)
	{
	case STATE_ASK:
		state = (answer == "yes") ? STATE_R : STATE_END;
		break;
	case STATE_R:
	{
		stringstream s(answer);
		size_t t;
		s >> t;
		if (t <= 255){
			color.red((Uint8) t);
			state = STATE_G;
		}
		break;
	}
	case STATE_G:
	{
		stringstream s(answer);
		size_t t;
		s >> t;
		if (t <= 255){
			color.green((Uint8) t);
			state = STATE_B;
		}
		break;
	}
	case STATE_B:
	{
		stringstream s(answer);
		size_t t;
		s >> t;
		if (t <= 255){
			color.blue((Uint8) t);
			color.alpha(255);
			state = STATE_END;
		}
		editor.setColor(color);
		break;
	}
	}
}

void ChangeColorMode::setup()
{
	ConsoleModeBase::setup();
	state = STATE_PRESENTATION;
}

}
/* namespace yep */
